home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / rs_form / rs_form.bas next >
Encoding:
BASIC Source File  |  1997-07-10  |  7.9 KB  |  245 lines

  1. Attribute VB_Name = "Resize_forms"
  2. 'Workfile:      RS_FORM.BAS
  3. 'Created:       07/10/97
  4. 'Author:        David Thieme
  5. 'Description:   This module provides the code needed to
  6. '               adjust the placement of all controls on
  7. '               a form. There are three public subs.
  8. '               How to use this module:
  9. '                   In a forms Resize event type
  10. '                       ResizeForm Me
  11. '                           - This will resize all controls
  12. '                             on the form to match new form size
  13. '                   You can save a default form size by calling
  14. '                       StoreFormPosition Me
  15. '                   You can restore a form to its original size or
  16. '                   the size that was stored using the StoreFormPosition
  17. '                   sub by calling
  18. '                       RestoreFormPosition Me
  19. 'Dependencies:  None
  20. 'Issues:        No known problems
  21. '                   Please E-Mail problems to davet@paonline.com
  22. Option Explicit
  23.  
  24. Type ctrObj
  25.     Name As String
  26.     Index As Long
  27.     Parrent As String
  28.     Top As Long
  29.     Left As Long
  30.     Height As Long
  31.     Width As Long
  32.     ScaleHeight As Long
  33.     ScaleWidth As Long
  34. End Type
  35.  
  36. Private FormRecord() As ctrObj
  37. Private ControlRecord() As ctrObj
  38. Private MaxForm As Long
  39. Private MaxControl As Long
  40.  
  41. Private Function ActualPos(plLeft As Long) As Long
  42.     If plLeft < 0 Then
  43.         ActualPos = plLeft + 75000
  44.     Else
  45.         ActualPos = plLeft
  46.     End If
  47. End Function
  48.  
  49. Private Function FindForm(pfrmIn As Form) As Long
  50. Dim i As Long
  51.     FindForm = -1
  52.     If MaxForm > 0 Then
  53.         For i = 0 To (MaxForm - 1)
  54.             If FormRecord(i).Name = pfrmIn.Name Then
  55.                 FindForm = i
  56.                 Exit Function
  57.             End If
  58.         Next i
  59.     End If
  60. End Function
  61.  
  62. Private Function AddForm(pfrmIn As Form) As Long
  63. Dim FormControl As Control
  64. Dim i As Long
  65.     ReDim Preserve FormRecord(MaxForm + 1)
  66.     FormRecord(MaxForm).Name = pfrmIn.Name
  67.     FormRecord(MaxForm).Top = pfrmIn.Top
  68.     FormRecord(MaxForm).Left = pfrmIn.Left
  69.     FormRecord(MaxForm).Height = pfrmIn.Height
  70.     FormRecord(MaxForm).Width = pfrmIn.Width
  71.     FormRecord(MaxForm).ScaleHeight = pfrmIn.ScaleHeight
  72.     FormRecord(MaxForm).ScaleWidth = pfrmIn.ScaleWidth
  73.     AddForm = MaxForm
  74.     MaxForm = MaxForm + 1
  75.     For Each FormControl In pfrmIn
  76.         i = FindControl(FormControl, pfrmIn.Name)
  77.         If i < 0 Then
  78.             i = AddControl(FormControl, pfrmIn.Name)
  79.         End If
  80.     Next FormControl
  81. End Function
  82.  
  83. Private Function FindControl(inControl As Control, inName As String) As Long
  84. Dim i As Long
  85.     FindControl = -1
  86.     For i = 0 To (MaxControl - 1)
  87.         If ControlRecord(i).Parrent = inName Then
  88.             If ControlRecord(i).Name = inControl.Name Then
  89.                 On Error Resume Next
  90.                 If ControlRecord(i).Index = inControl.Index Then
  91.                     FindControl = i
  92.                     Exit Function
  93.                 End If
  94.                 On Error GoTo 0
  95.             End If
  96.         End If
  97.     Next i
  98. End Function
  99.  
  100. Private Function AddControl(inControl As Control, inName As String) As Long
  101.     ReDim Preserve ControlRecord(MaxControl + 1)
  102.     On Error Resume Next
  103.     ControlRecord(MaxControl).Name = inControl.Name
  104.     ControlRecord(MaxControl).Index = inControl.Index
  105.     ControlRecord(MaxControl).Parrent = inName
  106.     If TypeOf inControl Is Line Then
  107.         ControlRecord(MaxControl).Top = inControl.Y1
  108.         ControlRecord(MaxControl).Left = ActualPos(inControl.X1)
  109.         ControlRecord(MaxControl).Height = inControl.Y2
  110.         ControlRecord(MaxControl).Width = ActualPos(inControl.X2)
  111.     Else
  112.         ControlRecord(MaxControl).Top = inControl.Top
  113.         ControlRecord(MaxControl).Left = ActualPos(inControl.Left)
  114.         ControlRecord(MaxControl).Height = inControl.Height
  115.         ControlRecord(MaxControl).Width = inControl.Width
  116.     End If
  117.     If TypeOf inControl Is DBList Then
  118.         inControl.IntegralHeight = False
  119.     End If
  120.     On Error GoTo 0
  121.     AddControl = MaxControl
  122.     MaxControl = MaxControl + 1
  123. End Function
  124.  
  125. Private Function PerWidth(pfrmIn As Form) As Long
  126. Dim i As Long
  127.     i = FindForm(pfrmIn)
  128.     If i < 0 Then
  129.         i = AddForm(pfrmIn)
  130.     End If
  131.     PerWidth = (pfrmIn.ScaleWidth * 100) \ FormRecord(i).ScaleWidth
  132. End Function
  133.  
  134. Private Function PerHeight(pfrmIn As Form) As Single
  135. Dim i As Long
  136.     i = FindForm(pfrmIn)
  137.     If i < 0 Then
  138.         i = AddForm(pfrmIn)
  139.     End If
  140.     PerHeight = (pfrmIn.ScaleHeight * 100) \ FormRecord(i).ScaleHeight
  141. End Function
  142.  
  143. Private Sub ResizeControl(inControl As Control, pfrmIn As Form)
  144. Dim i As Long
  145. Dim yRatio, xRatio, lTop, lLeft, lWidth, lHeight As Long
  146.     yRatio = PerHeight(pfrmIn)
  147.     xRatio = PerWidth(pfrmIn)
  148.     i = FindControl(inControl, pfrmIn.Name)
  149.     On Error GoTo Moveit
  150.     If inControl.Left < 0 Then
  151.         lLeft = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000)
  152.     Else
  153.         lLeft = CLng((ControlRecord(i).Left * xRatio) \ 100)
  154.     End If
  155.     lTop = CLng((ControlRecord(i).Top * yRatio) \ 100)
  156.     lWidth = CLng((ControlRecord(i).Width * xRatio) \ 100)
  157.     lHeight = CLng((ControlRecord(i).Height * yRatio) \ 100)
  158.     GoTo Moveit
  159. Moveit:
  160.     On Error GoTo MoveError1
  161.     If TypeOf inControl Is Line Then
  162.         If inControl.X1 < 0 Then
  163.             inControl.X1 = CLng(((ControlRecord(i).Left * xRatio) \ 100) - 75000)
  164.         Else
  165.             inControl.X1 = CLng((ControlRecord(i).Left * xRatio) \ 100)
  166.         End If
  167.         inControl.Y1 = CLng((ControlRecord(i).Top * yRatio) \ 100)
  168.         If inControl.X2 < 0 Then
  169.             inControl.X2 = CLng(((ControlRecord(i).Width * xRatio) \ 100) - 75000)
  170.         Else
  171.             inControl.X2 = CLng((ControlRecord(i).Width * xRatio) \ 100)
  172.         End If
  173.         inControl.Y2 = CLng((ControlRecord(i).Height * yRatio) \ 100)
  174.     Else
  175.         If TypeOf inControl Is Timer Then
  176.             GoTo subExit
  177.         End If
  178.         If TypeOf inControl Is ImageList Then
  179.             GoTo subExit
  180.         End If
  181.         If TypeOf inControl Is CommonDialog Then
  182.             GoTo subExit
  183.         End If
  184.         inControl.Move lLeft, lTop, lWidth, lHeight
  185.     End If
  186.     GoTo subExit
  187. MoveError1:
  188.     On Error GoTo MoveError2
  189.     inControl.Move lLeft, lTop, lWidth
  190.     GoTo subExit
  191. MoveError2:
  192.     On Error GoTo subExit
  193.     inControl.Move lLeft, lTop
  194. subExit:
  195.     On Error GoTo 0
  196. End Sub
  197.  
  198. Public Sub ResizeForm(pfrmIn As Form)
  199. Dim FormControl As Control
  200. Dim isVisible As Boolean
  201. If pfrmIn.Top < 30000 Then
  202.     isVisible = pfrmIn.Visible
  203.     pfrmIn.Visible = False
  204.     For Each FormControl In pfrmIn
  205.         ResizeControl FormControl, pfrmIn
  206.     Next FormControl
  207.     pfrmIn.Visible = isVisible
  208. End If
  209. End Sub
  210.  
  211. Public Sub SaveFormPosition(pfrmIn As Form)
  212. Dim i As Long
  213.     If MaxForm > 0 Then
  214.         For i = 0 To (MaxForm - 1)
  215.             If FormRecord(i).Name = pfrmIn.Name Then
  216.                 FormRecord(i).Top = pfrmIn.Top
  217.                 FormRecord(i).Left = pfrmIn.Left
  218.                 FormRecord(i).Height = pfrmIn.Height
  219.                 FormRecord(i).Width = pfrmIn.Width
  220.                 Exit Sub
  221.             End If
  222.         Next i
  223.         AddForm (pfrmIn)
  224.     End If
  225. End Sub
  226.  
  227. Public Sub RestoreFormPosition(pfrmIn As Form)
  228. Dim i As Long
  229.     If MaxForm > 0 Then
  230.         For i = 0 To (MaxForm - 1)
  231.             If FormRecord(i).Name = pfrmIn.Name Then
  232.                 If FormRecord(i).Top < 0 Then
  233.                     pfrmIn.WindowState = 2
  234.                 ElseIf FormRecord(i).Top < 30000 Then
  235.                     pfrmIn.WindowState = 0
  236.                     pfrmIn.Move FormRecord(i).Left, FormRecord(i).Top, FormRecord(i).Width, FormRecord(i).Height
  237.                 Else
  238.                     pfrmIn.WindowState = 1
  239.                 End If
  240.                 Exit Sub
  241.             End If
  242.         Next i
  243.     End If
  244. End Sub
  245.